//
//  ProgressView.swift
//  SwiftChatUI
//
//  Created by xzk on 2019/2/26.
//  Copyright © 2019年 xzk. All rights reserved.
//

import UIKit

class ProgressView: UIView {
    
    private let inStartWH: CGFloat   = 50.0  // 内部白色圆形起始大小
    private let inEndWH: CGFloat     = 30.0  // 内部白色圆形长按大小
    
    private let outStartWH: CGFloat  = 70.0  // 外部白色圆形起始大小
    private let outEndWH: CGFloat    = 100.0  // 外部白色圆形长按大小

    
    override init(frame: CGRect) {
        super.init(frame: CGRect(x: frame.origin.x, y: frame.origin.y, width: 100, height: 100))
        
        addSubview(outCircleView)
        addSubview(inCircleView)
        addSubview(progressCircle)
    }
    
    public func start() {
        UIView.animate(withDuration: 0.25, animations: {
            var frame = self.inCircleView.frame
            frame.size.width = self.inEndWH
            frame.size.height = self.inEndWH
            
            self.inCircleView.frame = frame
            self.inCircleView.layer.cornerRadius = self.inEndWH * 0.5
            self.inCircleView.center = CGPoint(x: self.frame.size.width * 0.5, y: self.frame.size.height * 0.5)
            
            frame = self.outCircleView.frame
            frame.size.width = self.outEndWH
            frame.size.height = self.outEndWH
            
            self.outCircleView.frame = frame
            self.outCircleView.layer.cornerRadius = self.outEndWH * 0.5
            self.outCircleView.center = CGPoint(x: self.frame.size.width * 0.5, y: self.frame.size.height * 0.5)
        }) { (_) in
            self.progressCircle.startProgress()
        }
    }
    
    public func stop() {
        progressCircle.clearProgress()
        UIView.animate(withDuration: 0.25) {
            var frame = self.inCircleView.frame
            frame.size.width = self.inStartWH
            frame.size.height = self.inStartWH
            
            self.inCircleView.frame = frame
            self.inCircleView.layer.cornerRadius = self.inStartWH * 0.5
            self.inCircleView.center = CGPoint(x: self.frame.size.width * 0.5, y: self.frame.size.height * 0.5)
            
            frame = self.outCircleView.frame
            frame.size.width = self.outStartWH
            frame.size.height = self.outStartWH
            
            self.outCircleView.frame = frame
            self.outCircleView.layer.cornerRadius = self.outStartWH * 0.5
            self.outCircleView.center = CGPoint(x: self.frame.size.width * 0.5, y: self.frame.size.height * 0.5)
        }
    }
    
    
    private lazy var inCircleView: UIView = {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: inStartWH, height: inStartWH))
        view.center = CGPoint(x: self.frame.size.width * 0.5, y: self.frame.size.height * 0.5)
        view.backgroundColor = .white
        view.layer.cornerRadius = inStartWH * 0.5
        view.layer.masksToBounds = true
        
        return view
    }()
    
    private lazy var outCircleView: UIView = {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: outStartWH, height: outStartWH))
        view.center = CGPoint(x: self.frame.size.width * 0.5, y: self.frame.size.height * 0.5)
        view.backgroundColor = UIColor.init(red: 245 / 255.0, green: 245 / 255.0, blue: 245 / 255.0, alpha: 1.0)
        view.layer.cornerRadius = outStartWH * 0.5
        view.layer.masksToBounds = true
        
        return view
    }()
    
    private lazy var progressCircle: ProgressViewCircle = {
        return ProgressViewCircle()
    }()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class ProgressViewCircle: UIView {
    
    private let outEndWH: CGFloat    = 100.0  // 外部白色圆形长按大小
    
    // 进度
    private var maxTime: CGFloat        = 15.0
    private var progressValue: CGFloat  = 0.0
    private var currentTime: CGFloat    = 0.0
    
    private var animating: Bool = false
    
    init() {
        super.init(frame: CGRect(x: 0, y: 0, width: outEndWH, height: outEndWH))
        backgroundColor = .clear
    }
    
    override func draw(_ rect: CGRect) {
        guard let ctx: CGContext = UIGraphicsGetCurrentContext() else {
            debugPrint("context 加载失败")
            return
        }
        
        // 圆心
        let center: CGPoint = CGPoint(x: frame.size.width / 2.0, y: frame.size.height / 2.0)
        // 半径
        let radius: CGFloat = (outEndWH - 5) / 2.0
        // 起角
        let startAngle: CGFloat =  CGFloat(-Double.pi / 2)
        // 终角
        let endAngle: CGFloat = CGFloat(-Double.pi / 2 + Double.pi * 2.0 * Double(progressValue))
        
        let path: UIBezierPath = UIBezierPath(arcCenter: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
        ctx.setLineWidth(5)
        ctx.setStrokeColor(UIColor(red: 112 / 255.0, green: 183 / 255.0, blue: 99 / 255.0, alpha: 1.0).cgColor)
        ctx.addPath(path.cgPath)
        
        ctx.strokePath()
    }
    
    @objc public func startProgress() {
        animating = true
        isHidden = false
        animate()
    }
    
    public func clearProgress() {
        animating = false
        isHidden = true
        currentTime = 0
    }
    
    @objc private func animate() {
        if !animating {
            return
        }
        
        currentTime += 0.1
        let maxTime: CGFloat = CGFloat(self.maxTime)
        
        if maxTime > currentTime {
            progressValue = currentTime / maxTime
            setNeedsDisplay()   // 自动调用drawRect
            perform(#selector(animate), with: nil, afterDelay: 0.1)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
